<?php
/**
 * Request Class
 *
 * @author Matt Pike
 * @package Mhtml
 */
class Request{

	/**
	 * Get Paramater (unsanitised)
	 *
	 * @param string $name
	 * @param array|string $src
	 * @param mixed $default
	 * @return mixed
	 */
	public static function getParam($name,$default=null,$src="REQUEST"){
		$src = self::getSrc($src);
		return isset($src[$name]) ? $src[$name] : $default;
	}
	
	/**
	 * Return param only if letters
	 * 
	 * @param $name
	 * @param $default
	 */
	public static function getWord($name,$default=null,$src="REQUEST"){
		$src = self::getSrc($src);
		return isset($src[$name]) ? Filter::alpha($src[$name]) : $default;
	}

	/**
	 * Get integer
	 *
	 * @param string $name
	 * @param array|string $src
	 * @param mixed $default
	 * @return int
	 */
	public static function getInt($name,$src="REQUEST",$default=null){
		$src = self::getSrc($src);
		return isset($src[$name]) ? intval($src[$name]) : intval($default);
	}
	
	/**
	 * Get flag
	 *
	 * @param string $name
	 * @param array|string $src
	 * @param mixed $default
	 * @return boolean
	 */
	public static function getFlag($name,$src="REQUEST",$default=null){
		return self::getInt($name,$src,$default) ? true : false;
	}

	/**
	 * Get float
	 *
	 * @param string $name
	 * @param array|string $src
	 * @return float
	 */
	public static function getFloat($name,$src="REQUEST",$default=null){
		$src = self::getSrc($src);
		return isset($src[$name]) ? floatval($src[$name]) : floatval($default);
	}

	protected static function getSrc($src){
		if(!is_array($src)){
				switch($src){
				case "GET":
				case "_GET":
					$src =& $_GET;
					break;
				case "POST":
				case "_POST":
					$src =& $_POST;
					break;
				case "REQUEST":
				case "_REQUEST":
				default:
						$src =& $_REQUEST;
			}
		}
		return $src;
	}

	public static function set($key,$value){
		$_REQUEST[$key] = $_POST[$key] = $_GET[$key] = $value;
	}
	
	public static function isAjaxRequest(){
		return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest') ? true : false;
	}
}
